﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ALinq.BBS.DataAccess;
using ALinq.BBS.App_GlobalResources;

namespace ALinq.BBS.Controllers
{
    public partial class AdminController : Controller
    {
        public enum Event
        {
            Add,
            Update,
            Delete,
        }

        [HttpGet]
        public ActionResult GetBoards()
        {
            using (var boards = new Boards())
            {
                var q = boards.Select(o => new { o.ID, o.Name, ForumsCount = o.Forums.Count });
                return Json(q, JsonRequestBehavior.AllowGet);
            }

        }

        [HttpPost]
        public ActionResult BoardsList(Board board)
        {
            if (board.ID == 0)
            {
                using (var boards = new Boards())
                {
                    boards.AddItem(board.Name);
                    return RedirectToAction("BoardsList");
                }
            }
            else
            {
                using (var boards = new Boards())
                {
                    boards.UpdateItem(board.ID, board.Name);
                    return RedirectToAction("BoardsList");
                }
            }
            return View();
        }

        [HttpGet]
        public ActionResult BoardsList(int? deleteID, int? editID)
        {
            ViewBag.EditItemID = editID;
            if (deleteID != null)
            {
                using (var boards = new Boards())
                {
                    boards.DeleteItem(deleteID.Value);
                    return RedirectToAction("BoardsList");
                }
            }
            return View();
        }

        public ActionResult ForumsList(int? boardID, int? deleteItemID, int? editItemID)
        {
            ViewBag.EditItemID = editItemID;
            ViewBag.DeleteItemID = deleteItemID;

            var boards = new Boards();
            ViewBag.Boards = boards;
            if (boardID != null)
            {
                var board = boards.Single(o => o.ID == boardID);
                //ViewBag.Forums = board.Forums;
                ViewBag.CurrentBoard = board;

                if (editItemID != null)
                {
                    var forum = board.Forums.Single(o => o.ID == editItemID);
                    return View(forum);
                }

                if (deleteItemID != null)
                {
                    board.Forums.DeleteItems(new[] { deleteItemID.Value });
                }
            }



            return View();
        }

        [HttpPost]
        public ActionResult ForumsList(int boardID, int? id, string name)
        {
            var boards = new Boards();
            var board = boards.Single(o => o.ID == boardID);
            ViewBag.CurrentBoard = board;
            ViewBag.Boards = boards;

            if (id == null)
            {
                board.Forums.AddItem(name);
            }
            else
            {
                board.Forums.UpdateItem(id.Value, name);
            }

            return RedirectToAction("ForumsList", new { BoardID = boardID });
        }

        //[HttpGet]
        //public ActionResult ForumsList(int? boardID, int? id)
        //{
        //    if(id != null)
        //    {
        //        //TODO:Delete Fourm
        //        return RedirectToAction("ForumsList", new { boardID });
        //    }
        //    return View();
        //}
    }
}
